<?php

/**
 * CategoryController
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class Administrator_CategoryController extends Zend_Controller_Action {
	
	/**
	 * @see Zend_Controller_Action::init()
	 *
	 */
	public function init() {
		$this->view->baseUrl = $this->_request->getBaseURL ();
		$this->view->contextPath = "administrator/category";
		$this->view->pageTitle = "eway.vn: Category Management";
	}
	
	/**
	 * @see Zend_Controller_Action::preDispatch()
	 *
	 */
	public function preDispatch() {
	}
	
	/**
	 * The default action - show the home page
	 */
	public function indexAction() {
		$itemPerPage = 30;
		
		// Get list of existing categories for displaying
		$section = new SectionModel ( );
		$category = new CategoryModel ( );
		
		$sections = $section->fetchAll ();
		$currentSection = ( int ) $this->_request->getParam ( "section" );
		
		if ($currentSection < 1) {
			if (($sections != null) && ($sections->count () > 0)) {
				$currentSection = $sections->getRow ( 0 )->sec_id;
			}
		}
		
		if ($currentSection < 1) {
			return;
		}
		
		$totalCategories = $category->fetchAll ( "sec_id=" . $currentSection )->count ();
		$currentPage = ( int ) $this->_request->getParam ( "page" );
		$totalPages = floor ( ($totalCategories / $itemPerPage) + ((($totalCategories % $itemPerPage) == 0) ? 0 : 1) );
		
		if (($currentPage < 1) || ($currentPage > $totalPages)) {
			$currentPage = 1;
		}
		
		// Prepare for the view...
		$this->view->currentSection = $currentSection;
		$this->view->sections = $sections;
		$this->view->categories = $category->fetchAll ( "sec_id=" . $currentSection, "cat_position ASC", $itemPerPage, ($currentPage - 1) * $itemPerPage );
		
		$this->view->count = $totalCategories;
		$this->view->pages = $totalPages;
	}
	
	/**
	 * Add a new category to database
	 *
	 */
	public function addAction() {
		$position = 1;
		
		$category = new CategoryModel ( );
		$section = new SectionModel ( );
		
		if ($this->_request->isPost ()) {
			
			$sectionID = $this->_request->getPost ( "section" );
			$name = $this->_request->getPost ( "name" );
			$description = $this->_request->getPost ( "description" );
			
			// Prepare the position
			$position = $category->fetchAll ( "sec_id=" . $sectionID )->count () + 1;
			$data = array ("sec_id" => $sectionID, "cat_name" => $name, "cat_description" => $description, "cat_position" => $position );
			$category->insert ( $data );
			
			$this->_redirect ( "administrator/category" );
			return;
		}
		
		$this->view->sections = $section->fetchAll ();
		$this->view->category = $category->createRow ();
		$this->view->buttonText = "Add new";
		$this->view->action = "add";
	}
	
	/**
	 * Edit an existing category
	 *
	 */
	public function editAction() {
		
		$category = new CategoryModel ( );
		
		// Process category update
		if ($this->_request->isPost ()) {
			
			$filter = new Zend_Filter_StripTags ( );
			
			$id = ( int ) $this->_request->getPost ( "id" );
			
			if ($id < 0) {
				$this->_redirect ( "administrator/category" );
				return;
			}
			
			$name = $filter->filter ( $this->_request->getPost ( "name" ) );
			$description = $filter->filter ( $this->_request->getPost ( "description" ) );
			$sectionID = ( int ) $this->_request->getPost ( "section" );
			
			if (($name != '') && ($sectionID > 0)) {
				$data = array ("cat_id" => $id, "sec_id" => $sectionID, "cat_name" => $name, "cat_description" => $description );
				
				$category->update ( $data, "cat_id=" . $id );
			}
			
			$this->_redirect ( "administrator/category" );
			return;
		
		} else {
			
			// Find the selected category to update
			$id = ( int ) $this->_request->getParam ( "id" );
			$this->view->category = $category->fetchRow ( "cat_id=" . $id );
			
			$section = new SectionModel ( );
			$this->view->sections = $section->fetchAll ();
			
			// Form attributes
			$this->view->action = "edit";
			$this->view->buttonText = "Update";
		}
	}
	
	/**
	 * Delete a category
	 *
	 */
	public function deleteAction() {
		// Find the selected category to update
		$id = ( int ) $this->_request->getParam ( "id" );
		
		if ($id <= 0) {
			return;
		}
		
		$category = new CategoryModel ( );
		$category->delete ( "cat_id=" . $id );
		
		$this->_redirect ( "administrator/category" );
	}
	
	/**
	 * Change position for a category
	 *
	 */
	public function positionAction() {
		$id = ( int ) $this->_request->getParam ( "id" );
		
		if ($id <= 0) {
			return;
		}
		
		$position = ( int ) $this->_request->getParam ( "val" );
		
		if ($position <= 0) {
			$position = 1;
		}
		
		$data = array ("cat_position" => $position );
		
		$category = new CategoryModel ( );
		$category->update ( $data, "cat_id=" . $id );
		
		$this->_redirect ( "administrator/category" );
	}
}
